package com.dupt.web.controller.files;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import com.dupt.common.config.DUPTConfig;
import com.dupt.system.domain.vo.FileInfoVo;
import org.apache.commons.io.IOUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.dupt.common.annotation.Log;
import com.dupt.common.core.controller.BaseController;
import com.dupt.common.core.domain.AjaxResult;
import com.dupt.common.enums.BusinessType;
import com.dupt.system.domain.FileInfo;
import com.dupt.system.service.IFileInfoService;
import com.dupt.common.utils.poi.ExcelUtil;
import com.dupt.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件信息Controller
 * 
 * @author zhuanyu.sun
 * @date 2025-11-02
 */
@RestController
@RequestMapping("/file/info")
public class FileInfoController extends BaseController
{
    @Autowired
    private IFileInfoService fileInfoService;

    /**
     * 查询文件信息列表
     */
    @PreAuthorize("@ss.hasPermi('file:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(FileInfo fileInfo)
    {
        startPage();
        List<FileInfoVo> list = fileInfoService.selectFileInfoList(fileInfo);
        return getDataTable(list);
    }

    /**
     * 导出文件信息列表
     */
    @PreAuthorize("@ss.hasPermi('file:info:export')")
    @Log(title = "文件信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FileInfo fileInfo)
    {
        List<FileInfoVo> list = fileInfoService.selectFileInfoList(fileInfo);
        ExcelUtil<FileInfoVo> util = new ExcelUtil<FileInfoVo>(FileInfoVo.class);
        util.exportExcel(response, list, "文件信息数据");
    }

    /**
     * 获取文件信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('file:info:query')")
    @GetMapping(value = "/{fileId}")
    public AjaxResult getInfo(@PathVariable("fileId") Long fileId)
    {
        return success(fileInfoService.selectFileInfoByFileId(fileId));
    }

    /**
     * 新增文件信息
     */
    @PreAuthorize("@ss.hasPermi('file:info:add')")
    @Log(title = "文件信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestParam(value="fileInfoVo",required=false)String fileInfoVo,
                          @RequestParam(value="files",required=false) List<MultipartFile> files) throws IOException
    {
        return toAjax(fileInfoService.insertFileInfo(fileInfoVo,files));
    }

    /**
     * 修改文件信息
     */
    @PreAuthorize("@ss.hasPermi('file:info:edit')")
    @Log(title = "文件信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestParam(value="fileInfoVo",required=false)String fileInfoVo,
                           @RequestParam(value="files",required=false) List<MultipartFile> files) throws IOException
    {
        return toAjax(fileInfoService.updateFileInfo(fileInfoVo,files));
    }

    /**
     * 删除文件信息
     */
    @PreAuthorize("@ss.hasPermi('file:info:remove')")
    @Log(title = "文件信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{fileIds}")
    public AjaxResult remove(@PathVariable Long[] fileIds)
    {
        return toAjax(fileInfoService.deleteFileInfoByFileIds(fileIds));
    }

    /**
     * 文件预览
     */
    @PreAuthorize("@ss.hasPermi('file:info:preview')")
    @GetMapping("/preview/{file}/{fileInfoId}")
    @Log(title = "质量追溯", businessType = BusinessType.DOWNANDPREVIEW)
    public void 文件信息(@PathVariable("file") String file,@PathVariable("fileInfoId") Long fileInfoId, HttpServletResponse response) {
        try {
            // 根据fileInfoId获取文件信息
            FileInfoVo fileInfoVo = fileInfoService.selectFileInfoByFileInfoId(fileInfoId);
            // 获取文件绝对路径
            String filePath =  DUPTConfig.getUploadPath()+fileInfoVo.getFilePath();
            File localFile = new File(filePath+file);
            if (!localFile.exists()) {
                throw new RuntimeException("文件不存在");
            }

            ServletOutputStream outputStream = response.getOutputStream();
            InputStream in = new FileInputStream(localFile);// 读取文件
            int i = IOUtils.copy(in, outputStream); // copy流数据,i为字节数
            in.close();
            outputStream.close();
        } catch (IOException e) {
            throw new RuntimeException("文件预览失败", e);
        }
    }
}
